package com.eqdd.yiqidian.ui.gongneng.renshi.gonggao;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.RadioButton;

import com.eqdd.yiqidian.CreateNoticeActivityCustom;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector;
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.rebuild.MultiTreeBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditEnterBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditRadioBean;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.test.ExpandBean;
import com.eqdd.yiqidian.test.SelectExpandActivity;
import com.eqdd.yiqidian.ui.base.SlimBaseMultiEditActivity;
import com.eqdd.yiqidian.ui.select.SelectMultiBumenActivity;
import com.eqdd.yiqidian.utils.SingleChooseUtil;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.eqdd.yiqidian.widget.ActionSheetDialog;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;

public class CreateNoticeActivity extends SlimBaseMultiEditActivity {
    private CreateNoticeActivityCustom dataBinding;
    private ActionSheetDialog chooseDialogRelate;
    private String msg;
    private ExpandBean expandBean;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_create_notice);
        initTopTitleBar(View.VISIBLE, "发布公告");
        initTopRightText("发布", v -> {
            if (checkParam()) {
                boolean left = ((SlimEditRadioBean) datas.get(1)).isLeft();
                OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.CREATE_NOTICE)
                        .params("companyId", user.getCompanyId())
                        .params("userGuid", user.getGuid())
                        .params("noticeName", datas.get(0).getContent())
                        .params("objectType", left ? 0 : 1)
                        .params("objectDepartId", left ? 0 : expandBean.getId())
                        .params("noticeTheme", datas.get(left ? 2 : 3).getContent())
                        .params("noticeContent", datas.get(left ? 3 : 4).getContent())
                        .params("duty", datas.get(left ? 4 : 5).getContent())
                        .params("noticeCycle", datas.get(left ? 5 : 6).getContent())
                        .execute(new DialogCallBack<HttpResult>(this) {
                            @Override
                            public void onSuccess(Response<HttpResult> response) {
                                HttpResult httpResult = response.body();
                                ToastUtil.showShort(httpResult.getMsg());
                                if (httpResult.getStatus() == 200) {
                                    finish();
                                }
                            }
                        });
            }else {
                ToastUtil.showShort(msg);
            }
        });
    }

//    private String getDepartId() {
//
//        StringBuilder stringBuilder = new StringBuilder();
//        for (int i = multiTreeBeanList.size() - 1; i >= 0; i--) {
//            stringBuilder.append(multiTreeBeanList.get(i).getId() + ";");
//        }
//        return stringBuilder.deleteCharAt(stringBuilder.lastIndexOf(";")).toString();
//    }

    private boolean checkParam() {
        boolean left = ((SlimEditRadioBean) datas.get(1)).isLeft();
        if (TextUtils.isEmpty(datas.get(0).getContent())) {
            msg = "请输入公告名称";
            return false;
        }
        if (!left && TextUtils.isEmpty(datas.get(2).getContent())) {
            msg = "请选择部门";
            return false;
        }
        if (TextUtils.isEmpty(datas.get(left ? 2 : 3).getContent())) {
            msg = "请输入公告主题";
            return false;
        }
        if (TextUtils.isEmpty(datas.get(left ? 3 : 4).getContent())) {
            msg = "请选择公告内容";
            return false;
        }
        if (TextUtils.isEmpty(datas.get(left ? 4 : 5).getContent())) {
            msg = "请输入公告责任";
            return false;
        }
        if (TextUtils.isEmpty(datas.get(left ? 5 : 6).getContent())) {
            msg = "请选择公告周期";
            return false;
        }

        return true;
    }


    @Override
    protected RecyclerView initRecyclerView() {
        return dataBinding.recyclerView;
    }

    @Override
    public void itemClick(int p) {
        switch (slimAdapterEx.getDataItem(p).getPosition()) {
            case 2:
//                Bundle bundle = new Bundle();
//                bundle.putParcelableArrayList("list", multiTreeBeanList);
//                startActivityForResult(SelectMultiBumenActivity.class, bundle, Config.SELECT_MULTI_BUMEN);
                startActivityForResult(SelectExpandActivity.class, null, Config.SELECT_EXPAND);
                break;
            case 6:
                if (chooseDialogRelate == null) {
                    SingleChooseUtil.getChooseDialog(CreateNoticeActivity.this, 28, (sheet) -> {
                        chooseDialogRelate = sheet;
                        sheet.show();
                    }, (position, strings) -> {
                        slimAdapterEx.getDataItem(p).setContent(strings.get(position - 1));
                        slimAdapterEx.notifyItemChanged(p);
                    });
                } else {
                    chooseDialogRelate.show();
                }
                break;
        }
    }

    @Override
    protected void setEditBeans() {

        datas.add(new SlimEditEnterBean("公告名称", "", 0, "请输入公告名称"));
        datas.add(new SlimEditRadioBean("全体员工", "部门", true, 1));
        datas.add(new SlimEditEnterBean("主题", "", 3, "请输入公告主题"));
        datas.add(new SlimEditEnterBean("内容", "", 4, "请输入公告内容"));
        datas.add(new SlimEditEnterBean("责任", "", 5, "请输入公告责任"));
        datas.add(new SlimEditEnterBean("周期", "", 6, true, "请选择公告周期"));
        slimAdapterEx.register(R.layout.list_item_38, new SlimInjector<SlimEditRadioBean>() {
            @Override
            public void onInject(SlimEditRadioBean data, IViewInjector injector) {

                injector.text(R.id.rb_reject, data.getLeftContent())
                        .text(R.id.rb_agree, data.getRightContent())
                        .checked(data.isLeft() ? R.id.rb_reject : R.id.rb_agree, true)
                        .checked(data.isLeft() ? R.id.rb_agree : R.id.rb_reject, false)
                        .with(R.id.rb_reject, v -> {
                            ((RadioButton) v).setOnCheckedChangeListener((buttonView, isChecked) -> {
                                if (isChecked) {
                                    data.setLeft(true);
                                    injector.checked(R.id.rb_agree, !isChecked);
                                    if (slimAdapterEx.getDataItem(2).getPosition() == 2) {
                                        expandBean = null;
                                        slimAdapterEx.getData().remove(2);
                                        slimAdapterEx.notifyItemRemoved(2);
                                    }
                                }
                            });
                        })
                        .with(R.id.rb_agree, v -> {
                            ((RadioButton) v).setOnCheckedChangeListener((buttonView, isChecked) -> {
                                if (isChecked) {
                                    data.setLeft(false);
                                    injector.checked(R.id.rb_reject, !isChecked);
                                    if (slimAdapterEx.getDataItem(2).getPosition() != 2) {
                                        slimAdapterEx.getData().add(2, new SlimEditEnterBean("部门", "", 2, true, "请选择部门"));
                                        slimAdapterEx.notifyItemInserted(2);
                                    }
                                }
                            });
                        });
            }
        });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Config.SUCCESS && requestCode == Config.SELECT_EXPAND) {
            expandBean = data.getParcelableExtra("bean");
            slimAdapterEx.getDataItem(2).setContent(expandBean.getLabel());
            slimAdapterEx.notifyItemChanged(2);
        }
    }
}
